// pages/shop/address/add/add.js
var app = getApp();
import Api from '../../../../api/api.js';
const api = new Api();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: false,
    addDic:{},
    valueArray: [0, 0, 0],
    cityName: '',
    cityArray: [],
    name: "",
    mobile: "",
    detailAddress: "",
    isCanDo: true,
    isMoRen: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var json = wx.getStorageSync("addressDic");
    console.log(json);
    if (json.length){
      var dic = JSON.parse(json);
      this.setData({
        addDic:dic,
        name:dic.name,
        mobile: dic.mobile,
        cityName: dic.city,
        detailAddress: dic.address
      });
      if (dic.isDefault == 1){
        this.setData({
          isMoRen: true,
        });
      } else {
        this.setData({
          isMoRen:false,
        });
      }
    }else{
      wx.navigateBack();
      return;
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getAddressList() {
    var _this = this;
    var map = {};
    api.getRequestData(app.getCitysUrl, map, false).then(res => {
      var str = "'" + res.data.result + "'";
      var arr = JSON.parse(str);

      if (res.data.flag) {
      } else {
        app.alert(res.data.msg);
      }
    }).catch(err => {
      console.log(err);
    });
  },
  saveAdd() {
    if (!this.data.isCanDo) {
      return;
    }
    var _this = this;
    if (!this.data.name.length) {
      app.alert("请输入姓名");
      return;
    }
    if (!this.data.mobile.length) {
      app.alert("请输入手机号");
      return;
    }
    if (!(/^[1][3,6,4,5,7,8,9][0-9]{9}$/.test(this.data.mobile))) {
      app.alert("手机号码格式错误");
      return;
    }
    if (!this.data.cityName.length) {
      app.alert("请选择城市");
      return;
    }
    if (!this.data.detailAddress.length) {
      app.alert("请输入详细地址");
      return;
    }
    var map = {};
    map.id = this.data.addDic.id;
    map.mobile = this.data.mobile;
    map.name = this.data.name;
    map.city = this.data.cityArray.join(" ");
    map.appId = app.APP_ID;
    map.userId = app.USER_ID();
    map.openId = app.OPEN_ID();
    if (this.data.isMoRen) {
      map.isDefault = "1";
    } else {
      map.isDefault = "0";
    }
    map.address = this.data.detailAddress;
    this.data.isCanDo = false;
    api.getRequestData(app.addressEditUrl, map, false).then(res => {
      if (res.data.errorCode == 0) {
        app.showToasts("修改成功");
        app.settlementPageNeedFresh = true;
        var page = getCurrentPages();
        var upPage = page[page.length - 2];
        upPage.setData({
          isNeedFresh: true
        });
        setTimeout(function () {
          _this.data.isCanDo = true;
          wx.navigateBack();
        }, 1500);
      } else {
        _this.data.isCanDo = true;
        app.alert(res.data.msg);
      }
    }).catch(err => {
      _this.data.isCanDo = true;
    });
  },
  switchChange(e) {
    console.log(e.detail.value);
    this.data.isMoRen = e.detail.value;
  },
  nameInput(e) {
    this.data.name = e.detail.value;
  },
  mobileInput(e) {
    this.data.mobile = e.detail.value;
  },
  detailInput(e) {
    this.data.detailAddress = e.detail.value;
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  selectCityClick(e) {
    var value = e.detail.value;
    this.data.cityArray = value;
    this.setData({
      cityName: value.join(" "),
    });
  },
  deleteAddress(){
    if (!this.data.isCanDo) {
      return;
    }
    var _this = this;
    wx.showModal({
      title: '提示',
      content: "确定要删除这个地址吗？",
      success: function (res) {
        if (res.confirm) {
          _this.deleteAdd();
        } else if (res.cancel) { 

        }
      }
    })
  },
  deleteAdd(){
    var _this = this;
    var map = {};
    map.id = this.data.addDic.id;
    this.data.isCanDo = false;
    api.getRequestData(app.addressDeleteUrl, map, false,"GET").then(res => {
      if (res.data.errorCode == 0) {
        app.showToasts("删除成功");
        app.settlementPageNeedFresh = true;
        var page = getCurrentPages();
        var upPage = page[page.length - 2];
        upPage.setData({
          isNeedFresh: true
        });
        setTimeout(function () {
          _this.data.isCanDo = true;
          wx.navigateBack();
        }, 1500);
      } else {
        _this.data.isCanDo = true;
        app.alert(res.data.errorMsg);
      }
    }).catch(err => {
      _this.data.isCanDo = true;
    });
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})